/****赵晓莉***/
require(["jquery","template","bootstrap3.min","bbcare","scprit/followup/app/comm/page/page.js","scprit/department/view/template/monitorList-oxygen.js"],function($,template,bootstrap,$bb,$page,$tpl) {
    var URL = $bb.baseUrl("department")+"followup/service";
    var count=20;//每页展示的患者个数
    $bb.ajax({
        url: URL + "/patient/familyreport/patientDataStatistics",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {
                "type":"2"
            }
        }),
        success: function (data) {
            var watchList=$(".monitor-report-list>div");
            watchList.eq(0).find(".monitor-report-val").html(data.data.dayTestPatients);
            watchList.eq(1).find(".monitor-report-val").html(data.data.totalTestPatients);
            watchList.eq(2).find(".monitor-report-val").html(data.data.abnormalTimes);
            watchList.eq(3).find(".monitor-report-val").html(data.data.totalTestTimes);
            $(".monitor-report-footing").find(".totalNum").html(data.data.totalTestTimes);
        },
        error: function (data) {}
    });
    //姓名截取前三个字：
    function nameDeal(name){
        var username;
        var len=name.length;
        if(len>3){
            username=name.substring(0,3);
        }else{
            username=name
        }
        return username;
    }
    function timeLong(obj,attr,userName){
        for(var i=0;i<obj.length;i++){
            var duration;
            var username=obj[i][userName];
            var timeLong=obj[i][attr];//获得秒数；
            var d=parseInt(timeLong/(24*3600));
            var h=parseInt(timeLong/3600);
            var m=parseInt((timeLong%3600)/60);
            var s=parseInt((timeLong%3600)%60);
            if(d>=1){
                if(h>=1){
                    if(m>=1){
                        if(s>=1){
                            duration=d+"天"+h+"小时"+m+"分"+s+"秒";
                        }else{
                            duration=d+"天"+h+"小时"+m+"分";
                        }
                    }else{
                        if(s>=1){
                            duration=d+"天"+h+"小时"+s+"秒";
                        }else{
                            duration=d+"天"+h+"小时";
                        }
                    }
                }else{
                    if(m>=1){
                        if(s>=1){
                            duration=d+"天"+m+"分"+s+"秒";
                        }else{
                            duration=d+"天"+m+"分";
                        }
                    }else{
                        if(s>=1){
                            duration=d+"天"+s+"秒";
                        }else{
                            duration=d+"天";
                        }
                    }
                }
            }else{
                if(h>=1){
                    if(m>=1){
                        if(s>=1){
                            duration=h+"小时"+m+"分"+s+"秒";
                        }else{
                            duration=h+"小时"+m+"分";
                        }
                    }else{
                        if(s>=1){
                            duration=h+"小时"+s+"秒";
                        }else{
                            duration=h+"小时";
                        }
                    }
                }else{
                    if(m>=1){
                        if(s>=1){
                            duration=m+"分"+s+"秒";
                        }else{
                            duration=m+"分";
                        }
                    }else{
                        if(s>=1){
                            duration=s+"秒";
                        }else{
                            duration="暂无数据";
                        }
                    }
                }
            }
            obj[i][attr]=duration;
            obj[i][userName]=nameDeal(username);
        }
        return obj;
    };

    function getOxygen(page,rows){
         $bb.ajax({
             url: URL + "/patient/oxygenPulserate/patientListqry",
             type: "post",
             contentType: "application/json;charset=UTF-8",
             dataType: "json",
             data: JSON.stringify({
                 data: {
                     "page":page,
                     "rows":rows
                 }
             }),
             success: function (data) {
                var pages=Math.ceil(data.total/rows);
                var value=timeLong(data.rows,"timeLong","userName");
                $tpl.listInit({"id":"#monitor-user-box1","data":value});
                var emptyLis='';
                if(value.length<count){
                    for(var i=0;i<count-value.length;i++){
                        emptyLis+='<div class="monitor-user-card"></div>';
                    }
                }
                $("#monitor-user-box1").append(emptyLis);
                 $page.init({
                     id:"btn-toolbar",
                     pagesAll:pages,
                     page:page
                 });
                 $("#btn-toolbar .btn").click(function(){
                     var text = $(this).text();
                     page = $page.getPageActive({
                         "pageClick":text,
                         "page":page,
                         "pagesAll":pages
                     });
                     getOxygen(page,rows);
                 });
             },
             error: function (data) {}
         });
     }
    getOxygen("1",count);

    $("#monitor-user-box1").on("click",".monitor-user-card",function(){
        var userId=$(this).attr("id");
        var gender=$(this).attr("gender");
        var obj={
            "userId":userId,
            "gender":gender
        };
        if(userId){
            $bb.openPage("oxygenPulseFequency",obj);
        }
    });

    $("#getAll").click(function(){
        getOxygen("1",count);
        var key=$("#key").val("");
    });
    $(".input-group-addon").click(function(){
        var key=$("#key").val();
        search(key,count,1);
    });
    $("#key").on("input propertychange",function(){
        var key=$.trim($("#key").val());
        search(key,count,1);
    });
    function search(key,rows,page){
        $bb.ajax({
            url: URL + "/patient/oxygenPulserate/patientListqry",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data: {
                    "page":page,
                    "rows":rows,
                    "userName":key
                }
            }),
            success: function (data) {
                var pages=Math.ceil(data.total/rows);
                var value=timeLong(data.rows,"timeLong",'userName');
                $tpl.listInit({"id":"#monitor-user-box1","data":value});
                var emptyLis='';
                if(value.length<count){
                    for(var i=0;i<count-value.length;i++){
                        emptyLis+='<div class="monitor-user-card"></div>';
                    }
                }
                $("#monitor-user-box1").append(emptyLis);

                $page.init({
                    id:"btn-toolbar",
                    pagesAll:pages,
                    page:page
                });
                $("#btn-toolbar .btn").click(function(){
                    var text = $(this).text();
                    page = $page.getPageActive({
                        "pageClick":text,
                        "page":page,
                        "pagesAll":pages
                    });
                    search(key,rows,page);
                });
            },
            error: function (data){}
        });
    }
});